<template>
  <a-modal
    title="注册正样本"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          label="目标网关"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-select
            :showSearch="true"
            optionFilterProp='children'
            style="width: 100%"
            placeholder="请选择网关"
            @change="handleChange"
            v-decorator="['gatewayId', {rules: [{ required: true, message: '请选择网关！' }]}]">
            <a-select-option v-for="(item,i) in gatewayList" :key="i" :value="item.id">{{ item.gatewayName }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          label="通道"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-select
            :showSearch="true"
            optionFilterProp='children'
            style="width: 100%"
            placeholder="请选择通道"
            v-decorator="['channel', {rules: [{ required: true, message: '请选择通道！' }]}]">
            <a-select-option
              v-for="(item, i) in channelList" 　
              :key="i"
              :value="item.channel">
              {{ item.channel }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          label="正样本文件"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
          hidden
        >
          <a-input v-decorator="['fileIds']"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { gatewayListAll } from '@/api/modular/main/bizgateway/bizGatewayManage'
  import { sysDictTypeDropDown } from '@/api/modular/system/dictManage'
  import { registerNormalModel } from '@/api/modular/main/bizcollectionsamplemanage/bizCollectionSampleManageManage'
  import { listGatewayChannel } from '@/api/modular/main/bizgatewaychannel/bizGatewayChannelManage'

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        expectAccessTokenList: [],
        expectGroupIdList: [],
        gatewayList: [],
        ws: null,
        mTokenNum: '',
        collectFailureLevelList: [],
        btnLoading: false,
        gatewayId: 0,
        channelList: []
      }
    },
    methods: {
      init (record) {
        this.visible = true
        this.getGatewayList()
        this.getFaultLevelList()
        setTimeout(() => {
          this.form.setFieldsValue({
            fileIds: record.fileIds.toString()
          })
        }, 100)
      },
      getFaultLevelList () {
        sysDictTypeDropDown({ code: 'fault_level' }).then((res) => {
          if (res.success) {
            this.collectFailureLevelList = res.data
          }
        })
      },
      handleSubmit () {
        const that = this
        const { form: { validateFields } } = this
        validateFields((errors, values) => {
          if (!errors) {
            that.gatewayId = values.gatewayId
            that.confirmLoading = true
            registerNormalModel(values).then((res) => {
              this.confirmLoading = false
              if (res.success) {
                values.taskId = res.data
                values.type = 1
                this.$message.success('注册正样本任务下发成功')
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.info('注册正样本任务下发失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
          this.confirmLoading = false
        })
      },
      handleChange (values) {
        let param = { gatewayId: values }
        listGatewayChannel(param).then(res => {
          if (res.success) {
            this.channelList = res.data
          }
        })
      },
      getGatewayList () {
        gatewayListAll().then(res => {
          if (res.success) {
            this.gatewayList = res.data
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.gatewayId = 0
        this.channelList = []
        this.visible = false
      },
      sendText (param) {
        if (!this.ws) {
          this.$message.info('未连接到设备，请刷新页面，重新连接！')
          return
        }
        this.ws.send(JSON.stringify(param))
      }
    },
    destroyed () {
      if (this.ws) {
        this.ws.close()
      }
    }
  }
</script>
